﻿using System;
using System.Collections.Generic;
using DynConDevCorp.OpenArch.SmartData.Interfaces;
using DynConDevCorp.OpenArch.SmartData.Interfaces.EventArgClasses;

namespace DynConDevCorp.OpenArch.SmartData.Core.EntityTypeLevel
{
    public class SmartEntityTypeLevelLibrary
    {
        private SmartEntityTypeLevelLibrary()
        {
        }

        /// <summary>
        /// Gets the global.
        /// </summary>
        /// <value>The global.</value>
        public static SmartEntityTypeLevelLibrary Global { get { return s_GlobalInstance; } }

        private static readonly SmartEntityTypeLevelLibrary s_GlobalInstance = new SmartEntityTypeLevelLibrary();

        private readonly Dictionary<Type, SmartEntityTypeLevelData> m_EntityTypeLibrary = new Dictionary<Type, SmartEntityTypeLevelData>();


        public void RegisterFieldEffectForType<TData>(Type entityType, string fieldName, ISmartEffect<TData> effect)
        {
            SmartEntityTypeLevelData data = RegisterIfNecessary(entityType);
            data.RegisterFieldEffectForType(effect);
        }

        public void RegisterFieldEffectForType<TData>(Type entityType, string fieldName, EventHandler<ValueChangedEventArgs<TData>> eventHandler)
        {
            SmartEffect<TData> newEffect = new SmartEffect<TData>(eventHandler);
            RegisterFieldEffectForType(entityType, fieldName, newEffect);
        }

        public SmartEntityTypeLevelData RegisterIfNecessary(Type entityType)
        {
            SmartEntityTypeLevelData retVal;
            if (!m_EntityTypeLibrary.TryGetValue(entityType, out retVal))
            {
                retVal = new SmartEntityTypeLevelData(entityType);
                m_EntityTypeLibrary.Add(entityType, retVal);
            }
            return retVal;
        }

        public SmartEntityTypeLevelData Lookup(Type entityType)
        {
            return m_EntityTypeLibrary[entityType];
        }
    }
}
